<template>
  <div class="content">
    <div class="action-box">
      <router-link to="/address_add">
        <img src="~assets/img/mall/icon_add.png">
        添加新地址
      </router-link>
    </div>
    <ul class="address-list">
      <li class="address-item" v-for="(item,index) in list" :key="index">
        <div class="address-info">
          <p><span>{{item.receiver}}</span><span>{{item.phone}}</span></p>
          <p>{{item.area +　item.address}}</p>
        </div>
        <div class="address-action">
          <input type="checkbox" name="" :id="`checkbox-${item.id}`" v-model="item.status">
          <label :for="`checkbox-${item.id}`" @click.prevent="setDefault(item,index)">
            设置为默认地址
          </label>
          <div class="action-buttons">
            <router-link :to="`/address_edit/${item.id}`"><img src="~assets/img/mall/icon_edit.png" alt="">编辑</router-link>
            <a @click="deleteConfirm(item.id,index)"><img src="~assets/img/mall/icon_delete.png" alt="">删除</a>
          </div>
        </div>
      </li>
    </ul>
  </div>
</template>
<script>
import { Toast, Indicator, MessageBox } from 'mint-ui'
import { apiPost } from 'src/common/api.js'

export default {
  name:'adress-management',
  data() {
    return {
      list:[]
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    deleteConfirm(id,index) {
      MessageBox.confirm('是否确定要删除该地址？')
       .then((action) => {
         this.deleteItem(id,index)
       })
       .catch(() =>{

       })
    },
    deleteItem(id,index) {
      apiPost('del_address',{'address_id':id}).then((res) => {
        if (res.status === 0) {
          this.list.splice(index, 1)
        } else {
          console.log(res.msg)
        }
      })
    },
    setDefault(item,index) {
      if(item.status === 1){
        return false
      }
      Indicator.open({
        spinnerType: "fading-circle"
      });
      apiPost('set_default_address',{'address_id':item.id}).then((res) => {
        if (res.status === 0) {
          this.getList()
        } else {
          console.log(res.msg)
        }
      })
      .then(()=>{
        Indicator.close()
      })
    },
    getList() {
      apiPost('addresses',{}).then((res) => {
        if (res.status === 0) {
          this.list = res.data
        } else {
          console.log(res.msg)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .action-box{
    border-bottom: 1px solid #d2d2d2;
    a{
      display: block;
      height: 55px;
      line-height: 55px;
      font-size: 14px;
      color: #333;
      text-align: center;
      background: #fff;
      img{
        margin-right: .5em;
        width: 25px;
        height: 25px;
        object-fit: contain;
      }
    }
  }
  .address-list{
    padding: 10px 0;
    .address-item{
      margin-bottom: 10px;
      .address-info{
        // margin-bottom: 5px;
        padding: 15px;
        background: #fff;
        border-bottom: 1px solid #d2d2d2;
        p{
          display: flex;
          justify-content: space-between;
          align-items: center;
        }
      }
      .address-action{
        display: flex;
        justify-content: space-between;
        align-items: stretch;
        margin-bottom: 10px;
        padding: 15px;
        background: #fff;
        border-bottom: 1px solid #d2d2d2;
        input[type=checkbox]{
            display: none;
        }
        label{
          line-height: 25px;
        }
        label::before{
          content: '';
          display: inline-block;
          width: 12px;
          height: 12px;
          border: 1px solid #d2d2d2;
          border-radius: 50%;
          background: #f4f4f6;
          transition: all .3s ease;
        }
        input[type=checkbox]:checked+label::before{
          background-image: url('~assets/img/mall/icon_check.png');
          background-repeat: no-repeat;
          background-size: contain;
          background-position: 50% 50%;
        }
        .action-buttons{
          a{
            display: inline-block;
            line-height: 25px;
            color: #333;
            background: transparent;
            img{
              margin-right: .5em;
              width: 13px;
              height: 13px;
              object-fit: contain;
              vertical-align: text-top;
            }
          }
          a:first-child{
            margin-right: 15px;
          }
        }
      }
    }
  }
</style>


